package com.tony.chartdemo.fragment;

import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import com.tony.chartdemo.BaseFragment;
import com.tony.chartdemo.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import static com.github.mikephil.charting.utils.ColorTemplate.rgb;

/**
 * Created by Tony on 2016/7/22.
 */

public class BarChartFragment extends BaseFragment implements OnChartValueSelectedListener {
    private BarChart barChart;

    @Override
    public View returnView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_barchart, container, false);
        return view;
    }

    @Override
    public void initView(View view) {
        barChart = (BarChart) view.findViewById(R.id.barChart);
    }

    @Override
    protected void initData() {
        initBarChart();
    }

    private void initBarChart() {
        //是否能够缩放
        barChart.setPinchZoom(true);
        //设置是否每个树状图后是否有阴影
        barChart.setDrawBarShadow(false);
        //是否拖拽
        barChart.setDragEnabled(true);
        //描述
        barChart.setDescription("");
//        barChart.setDescriptionColor(getResources().getColor(R.color.colorPrimaryDark));
//        barChart.setDescriptionTextSize(22);
        //设置XY轴的动画
        barChart.animateXY(2000, 2000);

        //文字居中显示
        barChart.setFitBars(true);
        //如果有超过90个barChar在表中，那么就隐藏数值
        barChart.setMaxVisibleValueCount(90);
        barChart.setScaleXEnabled(false);
        XAxis xAxis = barChart.getXAxis();
        xAxis.setDrawGridLines(false);
        //设置一个间隔

        barChart.setOnChartValueSelectedListener(this);
        xAxis.setGranularity(1f);
        xAxis.setLabelCount(6);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);


        Legend l = barChart.getLegend();
        l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
        l.setForm(Legend.LegendForm.SQUARE);
        l.setFormSize(9f);
        l.setTextSize(11f);
        l.setXEntrySpace(4f);


        YAxis leftYaxis = barChart.getAxisLeft();
        leftYaxis.setDrawGridLines(true);
        leftYaxis.setGridLineWidth(Utils.convertDpToPixel(1));
        leftYaxis.setGridColor(getResources().getColor(android.R.color.background_dark));
//        leftYaxis.setLabelCount(5, false);

        YAxis rightYaxis =barChart.getAxisRight();
        rightYaxis.setDrawGridLines(false);
        setData();


    }

    private void setData() {
        List<BarEntry> barEntries = new ArrayList<>();
        Integer[] ints = {23, 32, 45, 67, 89};
        for (int i = 0; i < ints.length; i++) {
            DecimalFormat dft = new DecimalFormat("#0");

            BarEntry barEntry = new BarEntry(i + 1, Integer.valueOf(dft.format(ints[i])));
            barEntries.add(barEntry);
        }
        BarDataSet set1 = new BarDataSet(barEntries, "the weather");
        //设置每个树状图的颜色，一共有两个方法setColors和setColor
                ArrayList<Integer> colors = new ArrayList<>();
        int[] MATERIAL_COLORS = {
                rgb("#2ecc71"), rgb("#f1c40f"), rgb("#e74c3c"), rgb("#3498db")
        };
        set1.setColors(MATERIAL_COLORS);
//        set1.setColors(getResources().getColor(android.R.color.holo_orange_dark));

        if (barChart.getData() != null && barChart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            set1.setValues(barEntries);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();
        } else {
            List<BarEntry> barEntries1 = new ArrayList<>();

            Integer ints1[] = {32, 45, 67, 23, 78, 98};
            for (int i = 0; i < ints1.length; i++) {
                BarEntry barEntry = new BarEntry(i + 1, ints1[i]);
                barEntries1.add(barEntry);
            }
            BarDataSet set2 = new BarDataSet(barEntries1, "222");
            set2.setColor(getResources().getColor(android.R.color.holo_green_light));

            ArrayList<IBarDataSet> barDataSets = new ArrayList<>();
            barDataSets.add(set1);
//        barDataSets.add(set2);
            BarData barData = new BarData(barDataSets);
            //设置树状图的宽度
            barData.setBarWidth(0.5f);
            //设置树状图上的字的字号
            barData.setValueTextSize(10);
            barChart.setData(barData);
            barChart.invalidate();
        }
    }

    protected RectF mOnValueSelectedRectF = new RectF();

    @Override
    public void onValueSelected(Entry e, Highlight h) {
        if (e == null)
            return;

        RectF bounds = mOnValueSelectedRectF;
        barChart.getBarBounds((BarEntry) e, bounds);
        MPPointF position = barChart.getPosition(e, YAxis.AxisDependency.LEFT);

        Log.i("bounds", bounds.toString());
        Log.i("position", position.toString());

        Log.i("x-index",
                "low: " + barChart.getLowestVisibleX() + ", high: "
                        + barChart.getHighestVisibleX());

        MPPointF.recycleInstance(position);
    }

    @Override
    public void onNothingSelected() {

    }
}
